﻿using System;
using System.Linq;

namespace CLM.Gestao.Administrador.DAL
{
    public class LancamentoDAO<T> where T : TB_LANCAMENTO
    {
        PlanejamentoBIRDSEntities model = new PlanejamentoBIRDSEntities();

        public void Add(T pEntity)
        {
            model.TB_LANCAMENTO.AddObject(pEntity);
        }

        public void Save()
        {
            try
            {
                model.CommandTimeout = 0;
                model.SaveChanges();
                model = new PlanejamentoBIRDSEntities();
                //return pEntity;
            }
            catch (System.Exception ex)
            {
                Exportacao.Texto.LogErro(ex, "Salvando na TB_LANCAMENTO");
                throw new Exception(ex.Message);
            }
        }

        public void Delete(T pEntity)
        {
            model.TB_LANCAMENTO.Attach(pEntity);
            model.TB_LANCAMENTO.DeleteObject(pEntity);
        }

        public void Update(T pEntity)
        {
            model.TB_LANCAMENTO.Attach(pEntity);
            model.ObjectStateManager.
              ChangeObjectState
              (pEntity, System.Data.EntityState.Modified);
        }

        public IQueryable<T> GetAll()
        {
            model.CommandTimeout = 0;
            model = new PlanejamentoBIRDSEntities();
            return model.CreateObjectSet<T>();
        }

        public IQueryable<TB_LANCAMENTO> GetAllDespesas()
        {
            model.CommandTimeout = 0;
            model = new PlanejamentoBIRDSEntities();
            IQueryable<TB_LANCAMENTO> a = from l in model.TB_LANCAMENTO
                                          join d in model.TB_LANCAMENTO_DESPESA
                                          on l.ID_LANCAMENTO equals d.ID_LANCAMENTO
                                          select l;
            return a;
        }

        public IQueryable<TB_LANCAMENTO> GetAllVendas()
        {
            model.CommandTimeout = 0;
            model = new PlanejamentoBIRDSEntities();
            IQueryable<TB_LANCAMENTO> a = from l in model.TB_LANCAMENTO
                                          join d in model.TB_LANCAMENTO_VENDA
                                          on l.ID_LANCAMENTO equals d.ID_LANCAMENTO
                                          select l;
            return a;
        }

        public IQueryable<TB_LANCAMENTO> GetAllCustos()
        {
            model.CommandTimeout = 0;
            model = new PlanejamentoBIRDSEntities();
            IQueryable<TB_LANCAMENTO> a = from l in model.TB_LANCAMENTO
                                          join d in model.TB_LANCAMENTO_CUSTO
                                          on l.ID_LANCAMENTO equals d.ID_LANCAMENTO
                                          select l;
            return a;
        }

    }
}